<!--
 Copyright (c) 2016,2017,2018,2019,2020,2021,2022 Klaus Landsdorf (http://node-red.plus/)
 Copyright 2016 - Jason D. Harper, Argonne National Laboratory
 Copyright 2015,2016 - Mika Karaila, Valmet Automation Inc.
 All rights reserved.
 node-red-contrib-modbus

 @author <a href="mailto:klaus.landsdorf@bianco-royal.de">Klaus Landsdorf</a> (Bianco Royal)
-->
<script type="text/javascript">
  RED.nodes.registerType('modbus-write', {
    category: 'modbus',
    color: '#E9967A',
    defaults: {
      name: {value: ''},
      showStatusActivities: {value: false},
      showErrors: {value: false},
      showWarnings: {value: true},
      unitid: {value: ''},
      dataType: {value: '', required: true},
      adr: {value: '', required: true, validate: RED.validators.number()},
      quantity: {value: '', required: true, validate: RED.validators.number()},
      server: {type: 'modbus-client', required: true},
      emptyMsgOnFail: {value: false},
      keepMsgProperties: {value: false},
      delayOnStart: {value: false},
      startDelayTime: {value: ''}
    },
    inputs: 1,
    outputs: 2,
    align: "right",
    icon: 'modbus.png',
    align: 'right',
    paletteLabel: 'Modbus-Write',
    label: function () {
      return this.name || 'Modbus Write'
    },
    oneditprepare: function () {
      let previous = null
      let node = this
      let fcDataTypeSelector = $('#node-input-dataType')
      let quantitySelector = $('#node-input-quantity')
      let modbusWriteQuantityRow = $('#modbus-write-quantity')

      let tabs = RED.tabs.create({
        id: "node-input-modbus-tabs",
        onchange: function (tab) {
          $("#node-input-tabs-content").children().hide()
          $("#" + tab.id).show()
        }
      })

      tabs.addTab({
        id: "modbus-settings-tab",
        label: this._("modbus-contrib.tabs-label.settings")
      })

      tabs.addTab({
        id: "modbus-options-tab",
        label: this._("modbus-contrib.tabs-label.options")
      })

      if (this.delayOnStart) {
        $('#node-delay').show()
      }
      else {
        $('#node-delay').hide()
        $('#node-input-delayOnStart').prop('checked', false)
      }

      $('#node-input-delayOnStart').change(function () {
        if ($(this).is(':checked')) {
          $('#node-delay').show()
        } else {
          $('#node-delay').hide()
        }
      })

      fcDataTypeSelector.on('focus', function () {
        previous = this.value
      }).change(function () {
        if (previous == null) {
          previous = fcDataTypeSelector.val()
        }

        switch (fcDataTypeSelector.val()) {
          case 'Coil':
          case 'HoldingRegister':
            node.quantity.required = false
            quantitySelector.val(1)
            modbusWriteQuantityRow.hide()
            break
          default:
            node.quantity.required = true
            modbusWriteQuantityRow.show()
        }
      })
    }
  })
</script>


<script type="text/x-red" data-template-name="modbus-write">
    <div class="form-row">
        <ul style="background: #fff; min-width: 600px; margin-bottom: 20px;" id="node-input-modbus-tabs"></ul>
    </div>
    <div id="node-input-tabs-content" style="min-height: 170px;">
        <div id="modbus-settings-tab" style="display:none">
            <div class="form-row">
                <label for="node-input-name"><i class="icon-tag"></i> Name</label>
                <input type="text" id="node-input-name" placeholder="Name">
            </div>
            <div class="form-row">
                <label for="node-input-unitid"><i class="icon-bookmark"></i> <span data-i18n="modbus-contrib.label.unitId"></span></label>
                <input type="text" id="node-input-unitid">
            </div>
            <div class="form-row">
                <label for="node-input-dataType"><i class="icon-list"></i> <span data-i18n="modbus-contrib.label.functioncode"></span></label>
                <select id="node-input-dataType">
                    <option value="Coil">FC 5: Force Single Coil</option>
                    <option value="HoldingRegister">FC 6: Preset Single Register</option>
                    <option value="MCoils">FC 15: Force Multiple Coils</option>
                    <option value="MHoldingRegisters">FC 16: Preset Multiple Registers</option>
                </select>
            </div>
            <div class="form-row">
                <label for="node-input-adr"><i class="icon-bookmark"></i> <span data-i18n="modbus-contrib.label.address"></span></label>
                <input type="text" id="node-input-adr">
            </div>
            <div class="form-row" id="modbus-write-quantity">
                <label for="node-input-quantity"><i class="icon-bookmark"></i> <span data-i18n="modbus-contrib.label.quantity"></span></label>
                <input type="text" id="node-input-quantity" placeholder="1-65535">
            </div>
            <div class="form-row" id="node-delayonstart">
                <label for="node-input-delayOnStart" style="width:160px">
                <i class="fa fa-power-off"></i> <span data-i18n="modbus-contrib.label.delayOnStart"></span></label>
                <input type="checkbox" id="node-input-delayOnStart" style="width:40px">
            </div>
            <div class="form-row" id="node-delay">
                <label for="node-input-startDelayTime"><i class="fa fa-clock-o"></i> <span data-i18n="modbus-contrib.label.startDelayTime"></span></label>
                <input type="text" id="node-input-startDelayTime" placeholder="10" style="width:80px">
            </div>
            <div class="form-row">
                <label for="node-input-server"><i class="icon-bookmark"></i> <span data-i18n="modbus-contrib.label.server"></span></label>
                <input type="text" id="node-input-server">
            </div>
        </div>
        <div id="modbus-options-tab" style="display:none">
            <div class="form-row">
                <label style="min-width:190px" for="node-input-emptyMsgOnFail"><i class="fa fa-th"></i> <span
                data-i18n="modbus-contrib.label.emptyMsgOnFail"></span></label>
                <input type="checkbox" id="node-input-emptyMsgOnFail" style="max-width:30px">
            </div>
            <div class="form-row">
                <label style="min-width:190px" for="node-input-keepMsgProperties"><i class="fa fa-th"></i> <span
                data-i18n="modbus-contrib.label.keepMsgProperties"></span></label>
                <input type="checkbox" id="node-input-keepMsgProperties" style="max-width:30px">
            </div>
            <div class="form-row">
                <label style="min-width:190px" for="node-input-showStatusActivities"><i class="fa fa-th"></i> <span
                data-i18n="modbus-contrib.label.showActivities"></span></label>
                <input type="checkbox" id="node-input-showStatusActivities" style="max-width:30px">
            </div>
            <div class="form-row">
                <label style="min-width:190px" for="node-input-showErrors"><i class="fa fa-th"></i> <span
                data-i18n="modbus-contrib.label.showErrors"></span></label>
                <input type="checkbox" id="node-input-showErrors" style="max-width:30px">
            </div>
            <div class="form-row">
                <label style="min-width:190px" for="node-input-showWarnings"><i class="fa fa-th"></i> <span
                data-i18n="modbus-contrib.label.showWarnings"></span></label>
                <input type="checkbox" checked id="node-input-showWarnings" style="max-width:30px">
            </div>
        </div>
    </div>
</script>


<script type="text/x-red" data-help-name="modbus-write">
    <p><strong>
        If you have more than 10 nodes on one communication configuration,
        use the Modbus-Flex-Writer or think about multiple connections to your Modbus device, please!
    </strong></p>

    <p>Modbus TCP/Serial node triggered with <strong>msg.payload</strong> to write.</p>
    <p>Connects to a Modbus TCP or serial to write coils/registers at each incoming msg.</p>
    <p>
        <h4>Function codes currently supported include:</h4>

        <ul>
          <li>FC 5: Force Single Coil</li>
          <li>FC 6: Preset Single Register</li>
          <li>FC 15: Force Multiple Coils</li>
          <li>FC 16: Preset Multiple Registers</li>
        </ul>
    </p>
    <p>
        Choose a function code (FC) from the dropdown menu,
        select the coil/register start address (0:65535) and the quantity to write.
        Choose or edit the Modbus TCP/serial server configuration to specify the server to connect to.
    </p>

    <p>Unit-Id (0..255 tcp | 0..247 serial) - leave empty, otherwise it overrides the default Unit-ID of client
     config</p>

    <p>
        For FC 5, <strong>msg.payload</strong> must be a value of 1 or 0 or true or false.
        For FC 15, <strong>msg.payload</strong> must be an array[] of comma separated values true or false each.
        For FC 6, <strong>msg.payload</strong> must be a single value between 0:65535.
        For FC 16, <strong>msg.payload</strong> must be an array[] of comma separated values between 0:65535 each.
    </p>

     <p>Output 1: all given data, modbus response Buffer, input message</p>
     <p>Output 2: modbus response Buffer, all given data, input message</p>

</script>
